<?php
/**
 * Simple PHP Contact Form
 *
 * This file is part of the Simple PHP Contact Form which is subject to the New
 * BSD License {@see LICENSE} which you would be advised to read before using,
 * modifying or redistributing this software.
 *
 * PHP version 5.2
 *
 * @category  Simple-PHP-Contact-Form
 * @package   Application_Controllers
 * @author    jah <jah@jahboite.co.uk>
 * @copyright 2010 jah <jah@jahboite.co.uk>
 * @license   New BSD License {@see LICENSE}
 * @version   SVN: $Id$
 * @link      http://code.google.com/p/simple-php-contact-form/
 */


/**
 * The ErrorController handles routing errors and all exceptions thrown by this
 * application.  Routing errors will result in a HTTP 404 Page Not Found type
 * page view.  Exceptions are immediately logged by the ESAPI Intrusion Detector
 * and the client is redirected to a generic error message unless the display_errors
 * config is set in which case an HTTP 500 type page is displayed which will also
 * display the exception if displayExceptions is set true.
 *
 * @category  Simple-PHP-Contact-Form
 * @package   Application_Controllers
 * @author    jah <jah@jahboite.co.uk>
 * @copyright 2010 jah <jah@jahboite.co.uk>
 * @license   New BSD License {@see LICENSE}
 * @version   Release: @package_version@
 * @link      http://code.google.com/p/simple-php-contact-form/
 */
class ErrorController extends Zend_Controller_Action
{
    /**
     * The errorAction handles errors and exceptions.
     *
     * @return null
     */
    public function errorAction()
    {
        $this->getResponse()->clearBody();
        $errors = $this->_getParam('error_handler');

        switch ($errors->type) {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

            // 404 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(404);
            $this->view->message = 'The page requested was not found.';
            break;
        default:

            // Log exceptions. EnterpriseSecurityException were automagically logged
            // so they are not logged here.
            if ($errors->exception instanceof EnterpriseSecurityException === false) {
                ESAPI::getIntrusionDetector()->addException($errors->exception);
            }

            // application error - if display_errors is off then the client
            // is redirected to the index controller error action where a
            // generic error message will be rendered.
            $bootstrap = $this->getInvokeArg('bootstrap');
            if ($bootstrap->hasOption('phpsettings')) {
                $o = $bootstrap->getOption('phpsettings');
                if (   array_key_exists('display_errors', $o)
                    && $o['display_errors'] !== '1'
                ) {
                    if (Zend_Session::sessionExists()) {
                        $ns = new Zend_Session_Namespace('Contact');
                        $ns->error = true;
                    }
                    $this->_helper->getHelper('redirector')
                        ->setCode(303)
                        ->gotoSimple(
                            'error', 'index', null, $this->_request->getParams()
                        );
                    return;
                }
            }
            $this->getResponse()->setHttpResponseCode(500);
            $this->view->message = 'Application error';
        }

        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }

        $this->view->request = $errors->request;
    }
}
